<?php
namespace addon\qiandao\controller;
use app\common\controller\AddonBase;


class Qiandao extends AddonBase{
	
	
	
	/**
	 * 构造方法
	 */
	public function _initialize()
	{
	
		parent::_initialize();
		
		
		
	}
	
	
	/* 点击签到按钮*/
	public function qian(){
		
		$uid = is_login();
		
		if($uid==0){
			$this->jump([RESULT_ERROR,'还未登录']);
		}
		
		$config = $this->getConfig('qiandao');
		$num = rand($config['minnum'], $config['maxnum']);
		
		$data['system']=$num;
		$data['score']=$config['num'];
		$data['uid']=$uid;
		$data['status']=1;
		$data['create_time']=time();
		$data['type']=$config['scoretype'];
		
		
		$this->jump( $this->commonLogic->setname('qiandao')->dataAdd($data,false,'','签到成功','',function($result,$data){
			
			point_change($data['uid'], $data['type'], $data['system']+$data['score'], 1, 'qiandao');
			
			
		}));

	}
	public function showqian(){
		$scheme = is_ssl() ? 'https://' : 'http://'; //协议类型;
		
		
		empty($this->param['uid'])?$uid=0:$uid = $this->param['uid'];
		$config = $this->getConfig('qiandao');
		
		$nowuid = is_login();
		$this->assign('nowuid',$nowuid);
		
		if($uid>0){
			$uid = $nowuid;
		}
		
		$url = $scheme.$_SERVER['HTTP_HOST'].addons_url('qiandao://Qiandao/allqd',array('uid'=>$uid));
		
		
		$this->assign('url',$url);
		
		
		
		if($uid==$nowuid){
			$my=1;
		}else{
			$my=0;
		}
		$this->assign('my',$my);
		$this->assign('scorename',parse_config_attr(webconfig('scoretype_list'))[$config['scoretype']]);
		return $this->addonTemplate('qddating','index');
		
	}
	public function allqd(){

			$where['status'] =1;
			$orderstr = 'create_time desc';
			 $page = $this->param['page'];
			$uid =$this->param['uid'];
			 
			 if($uid>0){
			 	$where['uid']=$uid;
			 }
			 
			$list=db('qiandao')->where($where)->order($orderstr)->page($page.',10')->getList();
			
			 $scoretype = parse_config_attr(webconfig('scoretype_list'));
			if($list){
				$result = '';
				foreach ($list as $key =>$vo){
					$userinfo = db('user')->where(['id'=>$vo['uid']])->getRow();
					$total = $vo['score']+$vo['system'];
					if($userinfo){
						
						$result .= '<li><dd class="col-md-7">';
						$result .= '<a class="aw-user-name" data-id="'.$userinfo['id'].'" href="'.es_url('user/home',array('id'=>$userinfo['id'])).'" rel="nofollow">';
						$result .= '<img src="'.getheadurl($userinfo['userhead']).'" class="middleimg" alt="'.$userinfo['nickname'].'">&nbsp;&nbsp;'.$userinfo['nickname'];
						
						$result .= '</a> &nbsp;&nbsp;&nbsp;&nbsp;'.friendlyDate($vo['create_time']).' 签到成功';
						
						$result .= '</dd><dd class="col-md-5">签到赚取<span>'.$vo['score'].'</span>+ 系统赠送<span>'.$vo['system'].'</span>= '.$scoretype[$vo['type']].'<span>'.$total.'</span></dd></li>';
							
					}
					
					
					
				
		
				}
		
				if($result==''){
					$this->jump(RESULT_ERROR,'获取失败');
				}else{
					$this->jump(RESULT_SUCCESS,'获取成功','',['data'=>$result]);
				}
		
			}else{
				$this->jump(RESULT_ERROR,'获取失败');
			}
		
		
		
		}
}
